{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "euvncfhnncksmdiwxkcm\n",
      "cggfut\n",
      "yfuvbiu"
     ]
    }
   ],
   "source": [
    "with open(r'D:/电子知识库/Vescode/text.txt',encoding=\"utf-8\" ) as f:\n",
    "        for line in f:\n",
    "            print(line, end='')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(r'D:/电子知识库/Vescode/text.txt',\"w\") as f:\n",
    "    namelst = ['张三 ', '李四', '王五']\n",
    "\n",
    "\n",
    "\n",
    "    f.writelines(namelst)\n",
    "\n",
    "   "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "保存数据完成!\n"
     ]
    }
   ],
   "source": [
    "import json\n",
    "\n",
    "def main():\n",
    "    mydict = {\n",
    "        'name': '骆昊',\n",
    "        'age': 38,\n",
    "        'qq': 957658,\n",
    "        'friends': ['王大锤', '白元芳'],\n",
    "        'cars': [\n",
    "            {'brand': 'BYD', 'max_speed': 180},\n",
    "            {'brand': 'Audi', 'max_speed': 280},\n",
    "            {'brand': 'Benz', 'max_speed': 320}\n",
    "        ]\n",
    "    }\n",
    "    try:\n",
    "        with open(r'D:/电子知识库/Vescode/text.txt', 'w', encoding='utf-8') as f:\n",
    "            json.dumps(mydict)\n",
    "    except IOError as e:\n",
    "        print(e)\n",
    "    print('保存数据完成!')\n",
    "\n",
    "\n",
    "if __name__ == '__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [],
   "source": [
    "'''编写程序，将等比数列的前99行 1/2+1/4+1/8+1/16+...+1/2^N 的算式及和写到一个文本文件中。\n",
    "例如：\n",
    "\n",
    "1/2 = 0.5\n",
    "1/2+1/4 = 0.75\n",
    "1/2+1/4+1/8 = 0.875\n",
    "1/2+1/4+1/8+1/16 = ...\n",
    "....\n",
    "....\n",
    "1/2+1/4+1/8+...1/2^99 = ...'''\n",
    "import json\n",
    "n = 1\n",
    "m = []\n",
    "for i in range(99):\n",
    "    n = n*1/2\n",
    "    m.append(n)\n",
    "\n",
    "y = []\n",
    "for i in range(1,len(m)+1):\n",
    "    s = sum(m[:i])\n",
    "    y.append(s)\n",
    "\n",
    "\n",
    "a ='1/2'\n",
    "x = [a]\n",
    "for i in range(2,100):\n",
    "    b =f\"1/{int(pow(2,i))}\"\n",
    "    a = a+\"+\"+b\n",
    "    x.append(a)\n",
    "w = []\n",
    "for j in range(99):\n",
    "    z=\"{}={}\".format(x[j],y[j])\n",
    "    w.append(z)\n",
    "        \n",
    "def main():\n",
    "    with open(r'D:/电子知识库/Vescode/text.txt',\"w\") as f:\n",
    "        json.dump(w,f)\n",
    "    \n",
    "if  __name__ == '__main__':\n",
    "    main()\n",
    "\n",
    "\n",
    "    \n",
    "    \n",
    "\n",
    "\n",
    "\n",
    "    \n",
    "        \n",
    "\n",
    "        \n",
    "        \n",
    "      \n",
    "        \n",
    "\n",
    " \n",
    "  \n",
    "        \n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.0\n",
      "1.5\n",
      "1.6666666666666667\n",
      "1.6\n",
      "1.625\n",
      "1.6153846153846154\n",
      "1.619047619047619\n",
      "1.6176470588235294\n",
      "1.6181818181818182\n",
      "1.6179775280898876\n",
      "1.6180555555555556\n",
      "1.6180257510729614\n",
      "1.6180371352785146\n",
      "1.618032786885246\n",
      "1.618034447821682\n",
      "1.6180338134001253\n",
      "1.618034055727554\n",
      "1.6180339631667064\n",
      "1.6180339985218033\n",
      "1.618033985017358\n",
      "1.6180339901755971\n",
      "1.618033988205325\n",
      "1.618033988957902\n",
      "1.6180339886704431\n",
      "1.6180339887802426\n",
      "1.618033988738303\n",
      "1.6180339887543225\n",
      "1.6180339887482036\n",
      "1.6180339887505408\n",
      "1.6180339887496482\n",
      "1.618033988749989\n",
      "1.618033988749859\n",
      "1.6180339887499087\n",
      "1.6180339887498896\n",
      "1.618033988749897\n",
      "1.618033988749894\n",
      "1.6180339887498951\n",
      "1.6180339887498947\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "1.618033988749895\n",
      "32951280098\n"
     ]
    }
   ],
   "source": [
    "'''课后作业\n",
    "将初始半径为1的 斐波那契弧线（螺旋线） 的前50步的长度和，以及分步与上一步的长度比打印出来。'''\n",
    "a = 1\n",
    "b = 1\n",
    "sums = 0\n",
    "for _ in range(50):\n",
    "    sums +=a\n",
    "    a,b = b,a+b\n",
    "    print(f\"{b/a}\")\n",
    "print(sums)"
   ]
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "a4c6f6dca0b39727a1d31abc575f26c9e9c5e97275968b776bf7d6bafbe246da"
  },
  "kernelspec": {
   "display_name": "Python 3.8.8 ('base')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
